package com.jcsz.gulimall.cart.vo;

import java.math.BigDecimal;
import java.util.List;

/**
 * 整个购物车
 * 需要计算的属性重写他的get方法，每次刷新他都会重新计算，然后显示到页面上
 *
 * @className: Cart
 * @author: 一把Po吉他
 * @date: 2022/11/10 10:14
 * @version: 1.0
 **/
public class Cart {
    //购买的商品项
    private List<CartItem> items;
    //商品数量
    private Integer countNum;
    //商品品类数量
    private Integer countType;
    //购物车总价格
    private BigDecimal totalAmount;
    //减免价格
    private BigDecimal reduce = new BigDecimal("0");

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItem item : this.items) {
                count += item.getCount();
            }
        }
        return count;
    }


    public Integer getCountType() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItem item : this.items) {
                count += 1;
            }
        }
        return count;
    }


    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        //计算购物项总价
        if (items != null && items.size() > 0) {
            for (CartItem item : this.items) {
                // 商品选中后计算价格
                if (item.getChecked()) {
                    //拿到购物车中单个商品的总金额
                    //添加到购物总价中
                    amount = amount.add(item.getTotalPrice());
                }
            }
        }
        BigDecimal subtract = amount.subtract(this.getReduce());
        //减去优惠总价
        return subtract;
    }


    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
